﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DiBang.Common;
using DiBang.Sql;
using System.Data.SqlClient;

namespace ChangWuDiBang
{
    public partial class FrmDiBangQuery : Office2007Form
    {
        public UserInfo _UserInfo
        {
            get;
            set;
        }

        SqlLib sqllib = new SqlLib();
        SqlExtend sqlextend = new SqlExtend();

        public FrmDiBangQuery()
        {
            this.EnableGlass = false;
            InitializeComponent();
        }

        private void FrmDiBangQuery_Load(object sender, EventArgs e)
        {
            if (_UserInfo == null)
                return;

            //
            dtpFromDate.Value = DateTime.Now.AddDays(-7);
            dtpToDate.Value = DateTime.Now.AddDays(1);
            //配置权限
            if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Administrator))
            {
                btnEdit.Visible = true;
            }
            else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_User))
            {
                btnEdit.Visible = true;
            }
            else
            {
                btnEdit.Visible = false;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            SetGrid();
        }

        private void SetGrid()
        {
            string ssql = @"SELECT DIBANG_ID,CHENG_DATE,PIN_MING,CHE_HAO,MaoZhong,PiZhong,RongQiNum,RongQiZhong,JingZhong,
OTHER,IMAGE_ID,CompleteFlag,CREATE_USER_ID,CREATE_TIME,LAST_EVENT_NAME,LAST_EVENT_USER_ID,LAST_EVENT_TIMEKEY
FROM DB_DIBANG WHERE (CONVERT(DATETIME,CHENG_DATE) between {0}FROM_DATE and {0}TO_DATE)";
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("FROM_DATE",dtpFromDate.Value));
            cmd.Parameters.Add(new SqlParameter("TO_DATE",dtpToDate.Value));

            if (!_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Administrator))
            {
                ssql += " AND CREATE_USER_ID={0}GROUP_USER_ID";
                cmd.Parameters.Add(new SqlParameter("GROUP_USER_ID", _UserInfo.UserID));
            }
            if(!string.IsNullOrEmpty(txtCheHao.Text.Trim()))
            {
                ssql += " AND CHE_HAO={0}CHE_HAO";
                cmd.Parameters.Add(new SqlParameter("CHE_HAO",txtCheHao.Text.Trim()));
            }
            if (!string.IsNullOrEmpty(txtPingMing.Text.Trim()))
            {
                ssql += " AND PIN_MING={0}PIN_MING";
                cmd.Parameters.Add(new SqlParameter("PIN_MING", txtPingMing.Text.Trim()));
            }
            if (!string.IsNullOrEmpty(txtUserID.Text.Trim()))
            {
                ssql += " AND CREATE_USER_ID={0}CREATE_USER_ID";
                cmd.Parameters.Add(new SqlParameter("CREATE_USER_ID", txtUserID.Text.Trim()));
            }

            ssql += " ORDER BY CHENG_DATE DESC";

            cmd.CommandText = string.Format(ssql, ConstParam.DbChar);

            DataTable dt = sqllib.GetDataSet(cmd).Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                //Weight没有使用,数据用Base64加密
                //string strWeight = dr["WEIGHT"].ToString();
                //if (!string.IsNullOrEmpty(strWeight))
                //{
                //    dr["WEIGHT"] = Base64Encoding.UnBaseEnconding(strWeight);
                //}
                //毛重,皮重,容器重,净重使用Base64加密
                string strMaoZhong = dr["MAOZHONG"].ToString();
                if (!string.IsNullOrEmpty(strMaoZhong))
                {
                    dr["MAOZHONG"] = Base64Encoding.UnBaseEnconding(strMaoZhong);
                }
                string strPiZhong = dr["PIZHONG"].ToString();
                if (!string.IsNullOrEmpty(strPiZhong))
                {
                    dr["PIZHONG"] = Base64Encoding.UnBaseEnconding(strPiZhong);
                }
                string strRongQiZhong = dr["RONGQIZHONG"].ToString();
                if (!string.IsNullOrEmpty(strRongQiZhong))
                {
                    dr["RONGQIZHONG"] = Base64Encoding.UnBaseEnconding(strRongQiZhong);
                }
                string strJingZhong = dr["JINGZHONG"].ToString();
                if (!string.IsNullOrEmpty(strJingZhong))
                {
                    dr["JINGZHONG"] = Base64Encoding.UnBaseEnconding(strJingZhong);
                }
            }
            if (dt == null || dt.Rows.Count <= 0)
            {
                MessageBoxEx.Show("查询不到数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            dgvDiBang.DataSource = dt;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //如果不是Administrator或者User,则返回
            if (_UserInfo.UserGroup != ConstParam.UserGroup_Administrator
                && _UserInfo.UserGroup != ConstParam.UserGroup_User)
            {
                MessageBoxEx.Show("没有修改的权限,请确认!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (dgvDiBang.CurrentRow == null)
            {
                MessageBoxEx.Show("请先选择需要修改的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            DiBangInfo dibangInfo = new DiBangInfo();

            dibangInfo.Dibang_ID = dgvDiBang.CurrentRow.Cells["DIBANG_ID"].Value.ToString();
            dibangInfo.ChengZhongDate = dgvDiBang.CurrentRow.Cells["CHENG_DATE"].Value.ToString();
            dibangInfo.PinMing = dgvDiBang.CurrentRow.Cells["PIN_MING"].Value.ToString();
            dibangInfo.CheHao = dgvDiBang.CurrentRow.Cells["CHE_HAO"].Value.ToString();
            //dibangInfo.ChengZhong = dgvDiBang.CurrentRow.Cells["WEIGHT"].Value.ToString();
            dibangInfo.ChengZhongOther = dgvDiBang.CurrentRow.Cells["OTHER"].Value.ToString();

            dibangInfo.MaoZhong = dgvDiBang.CurrentRow.Cells["MAOZHONG"].Value.ToString();
            dibangInfo.PiZhong = dgvDiBang.CurrentRow.Cells["PIZHONG"].Value.ToString();
            dibangInfo.RongQiNum = dgvDiBang.CurrentRow.Cells["QONGQINUM"].Value.ToString();
            dibangInfo.RongQiZhong = dgvDiBang.CurrentRow.Cells["RONGQIZHONG"].Value.ToString();
            dibangInfo.JingZhong = dgvDiBang.CurrentRow.Cells["JINGZHONG"].Value.ToString();

            dibangInfo.ImageID = dgvDiBang.CurrentRow.Cells["IMAGE_ID"].Value.ToString();

            FrmDiBangEdit frm = new FrmDiBangEdit();
            frm._UserInfo = this._UserInfo;
            frm._DiBangInfo = dibangInfo;
            frm.ShowDialog();

            SetGrid();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (dgvDiBang.CurrentRow == null)
            {
                MessageBoxEx.Show("请先选择需要打印的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string image_id = dgvDiBang.CurrentRow.Cells["IMAGE_ID"].Value.ToString();
            string ssql = @"SELECT FIRST_PHOTO,SECOND_PHOTO FROM DB_IMAGE WHERE IMAGE_ID='" + image_id + "'";
            DataTable dtImage = sqllib.GetDataSet(ssql).Tables[0];
            byte[] byteFirstPic = null;
            if (dtImage != null && dtImage.Rows.Count > 0)
            {
                byteFirstPic = (byte[])dtImage.Rows[0]["FIRST_PHOTO"];
            }
            //Show出报表界面
            //CHENG_DATE,PIN_MING,CHE_HAO,WEIGHT,OTHER,IMAGE_ID,CREATE_USER_ID,CREATE_TIME,
            //LAST_EVENT_NAME,LAST_EVENT_USER_ID,LAST_EVENT_TIMEKEY
            DiBangPrintInfo dibangPrintInfo = new DiBangPrintInfo();
            dibangPrintInfo.PinMing = dgvDiBang.CurrentRow.Cells["PIN_MING"].Value.ToString();
            dibangPrintInfo.CheHao = dgvDiBang.CurrentRow.Cells["CHE_HAO"].Value.ToString();
            dibangPrintInfo.ChengZhongDate = dgvDiBang.CurrentRow.Cells["CHENG_DATE"].Value.ToString();

            dibangPrintInfo.MaoZhong = dgvDiBang.CurrentRow.Cells["MAOZHONG"].Value.ToString();
            dibangPrintInfo.PiZhong = dgvDiBang.CurrentRow.Cells["PIZHONG"].Value.ToString();
            dibangPrintInfo.RongQiNum = dgvDiBang.CurrentRow.Cells["QONGQINUM"].Value.ToString();
            dibangPrintInfo.RongQiZhong = dgvDiBang.CurrentRow.Cells["RONGQIZHONG"].Value.ToString();
            dibangPrintInfo.JingZhong = dgvDiBang.CurrentRow.Cells["JINGZHONG"].Value.ToString();

            if (byteFirstPic != null)
            {
                dibangPrintInfo.FirstPic = Convert.ToBase64String(byteFirstPic);
            }

            FrmDiBangPrint frmDiBangPrint = new FrmDiBangPrint();
            frmDiBangPrint._DiBangPrintInfo = dibangPrintInfo;
            frmDiBangPrint.ShowDialog();
        }

        private void btnHistory_Click(object sender, EventArgs e)
        {
            DiBangInfo dibangInfo = new DiBangInfo();
            if (dgvDiBang.CurrentRow != null)
            {
                dibangInfo.Dibang_ID = dgvDiBang.CurrentRow.Cells["DIBANG_ID"].Value.ToString();
                dibangInfo.PinMing = dgvDiBang.CurrentRow.Cells["PIN_MING"].Value.ToString();
                dibangInfo.CheHao = dgvDiBang.CurrentRow.Cells["CHE_HAO"].Value.ToString();
            }
            FrmDiBangHistory frm = new FrmDiBangHistory();
            frm._UserInfo = this._UserInfo;
            frm._DiBangInfo = dibangInfo;
            frm.ShowDialog();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
